package abg.core.mapper;

import abg.core.domain.chat.ChatFriendMsgVo;
import abg.core.domain.db.ChatMsg;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author leyili
 */
public interface ChatMsgMapper extends ZBMapper<ChatMsg> {

    /**
     * 查询消息列表 根据未读和发送时间排序
     *
     * @return a
     */
    @Select({
            "<script>",
            "SELECT * FROM (",
            "SELECT *, ROW_NUMBER() OVER (PARTITION BY chat_id ORDER BY ct DESC) AS rn",
            "FROM chat_msg",
            "WHERE chat_id IN",
            "<foreach collection='allIds' item='chatId' open='(' separator=',' close=')'>",
            "#{chatId}",
            "</foreach>",
            ") AS subquery",
            "WHERE rn = 1",
            "ORDER BY is_read ASC, ct DESC"
            , "</script>"
    })
    List<ChatFriendMsgVo> getAllChatMsg(@Param("allIds") List<String> allIds);


    /**
     * 获取所有我有未读消息的房间
     *
     * @return a
     */
    @Select("SELECT DISTINCT chat_id FROM chat_msg WHERE to_id = #{userId} AND is_read = '0' ")
    List<String> getUnReadChat(@Param("userId") String userId);


}